package com.nj.interfaces.actions.uploadpicture.dao;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.nj.framework.dao.StandardGenericDAO;

/**
 * 
* 项目名称：publicservice   
* 类名称：UserPictureDAO   
* @version
* 类描述：用户图片管理dao
* @version   
* 创建人：娄安   
* @version
* 创建时间：2014年6月26日 上午9:47:41 
* @version  
* 修改人：娄安    修改时间：2014年6月26日 上午9:47:41   
* @version
* 修改备注：   
*
 */
@Repository
public class UserPictureDAO extends StandardGenericDAO {

	/**
	 * 
	* selectBusyCode 根据业务编码查找对应图片的前缀
	* @param    
	* @return  
	* @throws 
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 */
/*	public String selectBusyCode(String busyId) {
		String sql = "SELECT BUSY_CODE_CODE FROM ECM_BUSY_CODE WHERE PK_BUSY_CODE_ID=?";
		List<String> list = findAllList(sql, String.class, new Long(busyId));
		return (CollectionUtils.isNotEmpty(list) ? list.get(0) : null);
	}
	*/
	/**
	 * 
	* selectBusyInfo  根据图片类型返回业务信息
	* @param    
	* @return  
	* @throws 
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 */
	public Map<String, Object> selectBusyInfo(BigDecimal imageClassId) {
		String sql = "SELECT EBC.PK_BUSY_CODE_ID,EBC.BUSY_CODE_CODE FROM CMS_IMAGE_CLASSES CIC,ECM_BUSY_CODE EBC ";
		sql += "WHERE CIC.FK_BUSY_ID=EBC.PK_BUSY_CODE_ID AND CIC.PK_IMAGE_CLASS_ID=?";
		List<Map<String, Object>> list = findAllList(sql, imageClassId);
		return (CollectionUtils.isNotEmpty(list) ? list.get(0) : null);
	}
	
	/**
	 * 
	* selectImagePK  查找下个个生成图片的ID
	* @param    
	* @return  
	* @throws 
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 */
	public Long selectImagePK() {
		Long pk = null;
		String sql = "SELECT CMS_IMAGES_S.NEXTVAL FROM DUAL";
		try {
			pk =  findByObject(sql,Long.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pk;
	}
	
	/**
	 * 
	* insertDBImage  图片信息入库
	* @param    
	* @return  
	* @throws 
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 */
	public void insertDBImage(Long imagePK, String folderId, String busyCode, String userId, String imageName, String originalPath, 
			String newPath, String sourceId, String imageType, Long imageSize, String imageDesc, String imageClassId,
			BigDecimal width, BigDecimal height, String iconPath, String encryptPath) {
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("INSERT INTO CMS_IMAGES(PK_IMAGE_ID,FK_FOLDER_ID,FK_BUSY_CODE_ID,FK_USER_ID,");
		sqlBuilder.append("IMAGE_NAME,ORIGINAL_PATH,NEW_PATH,SOURCE_ID,IMAGE_TYPE,IMAGE_SIZE,IMAGE_DESC,");
		sqlBuilder.append("CREATED_DATE,CREATED_BY,LAST_UPDATED_DATE,LAST_UPDATED_BY, FK_IMAGE_CLASS_ID,");
		sqlBuilder.append("FK_IMAGE_SCENE_ID,WIDTH,HEIGHT,QUALITY,ICON_PATH,ENCRYPT_PATH) ");
		sqlBuilder.append("VALUES(?,?,?,?,?,?,?,?,?,?,?,SYSDATE,?,SYSDATE,?,?,?,?,?,?,?,?)");
		
		Long upUserId = null;
		if(userId != null) {
			upUserId = new Long(userId);
		}
		BigDecimal uploadSceneId = selectUploadHeadPicScene();//查看上传头像图片的场景ID
		BigDecimal quality = new BigDecimal(1);
		
		insert(sqlBuilder.toString(), imagePK, new Long(folderId), new Long(busyCode), new Long(userId), 
				imageName, originalPath, newPath, new Long(sourceId), imageType, imageSize + "", 
				imageDesc, upUserId, new Long(userId), new Long(imageClassId),
				uploadSceneId, width, height, quality, iconPath, encryptPath);
	}
	/*
	*//**
	 * 
	* selectImagePath  查询已有图片的路径
	* @param    
	* @return  
	* @throws 
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 *//*
	public String selectImagePath(String imagePk) {
		String sql = "SELECT CI.ORIGINAL_PATH FROM CMS_IMAGES CI WHERE CI.PK_IMAGE_ID=?";
		List<String> list = findAllList(sql, String.class, imagePk);
		return (CollectionUtils.isNotEmpty(list) ? list.get(0) : "");
	}
	
	*//**
	 * 
	* selectBindImagePath 获取用户与第三方绑定的图片  
	* @param    
	* @return  
	* @throws 
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 *//*
	public String selectBindImagePath(BigDecimal userId) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT EMAB.OPEN_HEAD FROM ECM_MEMBER_ACCOUNT_BINDS EMAB ");
		sb.append("WHERE EMAB.OPEN_HEAD IS NOT NULL AND EMAB.FK_USER_ID=? ORDER BY BIND_DATE DESC");
		
		List<String> list = findAllList(sb.toString(), String.class, userId);
		return (CollectionUtils.isNotEmpty(list) ? list.get(0) : "");
	}
	
	*//**
	 * 
	* selectUserHeadId  查询用户现有图片id
	* @param    
	* @return  
	* @throws 
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 *//*
	public String selectUserHeadId(String userId) {
		String sql = "SELECT EM.HEAD_ID FROM ECM_MEMBERS EM WHERE EM.FK_USER_ID=?";
		List<String> list = findAllList(sql, String.class, userId);
		return (CollectionUtils.isNotEmpty(list) ? list.get(0) : "");
	}
	*/
	/**
	 * 
	* updateUserHeaderId  更新用户头像图片
	* @param    
	* @return  
	* @throws 
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 */
	public void updateUserHeaderId(String userId, String headId) {
		String sql = "UPDATE ECM_MEMBERS EM SET EM.HEAD_ID=? WHERE EM.FK_USER_ID=?";
		update(sql, headId, userId);
	}
	
	/**
	 * 
	* selectUploadHeadPicScene  查询上传用户头像图片的场景
	* @param    
	* @return  
	 * @throws ClassNotFoundException 
	 * @throws DataAccessException 
	* @throws 
	* @since  CodingExample　Ver(编码范例查看) 1.1
	 */
	public BigDecimal selectUploadHeadPicScene()  {
		String sql = "SELECT CIS.PK_IMAGE_SCENE_ID FROM CMS_IMAGE_SCENES CIS WHERE CIS.IMAGE_SCENE_CODE='UPLOAD_HEAD_PIC'";
		try {
			BigDecimal sceneId = findByObject(sql, BigDecimal.class);
			return (sceneId != null ? sceneId : new BigDecimal(1));
			
		}catch(Exception e) {
			return null;
		}
	}
	
}
